package cloudspace.vm.javassist;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * The Class TextLogger. This is a standard text logger that uses a file as a
 * back end.
 */
public class TextLogger
{

	/** The log file. */
	private File logFile;

	/**
	 * Instantiates a new text logger.
	 * 
	 * @param filename
	 *            the filename to use as a backend.
	 */
	public TextLogger(String filename)
	{
		try
		{
			logFile = new File(filename);
			if (!logFile.exists())
			{
				logFile.createNewFile();
			}
		}
		catch (IOException e)
		{
			System.err.println("LOGGING NOT AVAILABLE.  FILE GIVEN IS BAD!!!!");
			e.printStackTrace();
		}
	}
	/**
	 * Log a line to this logger.
	 * 
	 * @param str
	 *            the string to log
	 */
	public void log(String str)
	{
		System.out.println(str);
		try
		{
			FileWriter tempWriter = new FileWriter(logFile);
			tempWriter.append(str);
			tempWriter.append("\n");
			tempWriter.flush();
		}
		catch (IOException e)
		{
			System.err.println("LOGGING NOT AVAILABLE.  FILE GIVEN IS BAD!!!!");
			e.printStackTrace();
		}
	}
}
